package com.duanjw.java;

import java.util.ArrayList;
import java.util.List;

public class Dispatcher {
    public Dispatcher() {
        this.addHandler(new ApiRequestHandler());
        this.addHandler(new StaticRequestHandler());
    }

    List<RequestHandler> requestHandlers = new ArrayList<>();

    public void addHandler(RequestHandler requestHandler) {
        requestHandlers.add(requestHandler);
    }

    /**
     * 处理请求
     *
     * @param s 请求的url
     * @return 处理后的结果
     */
    public Object handler(String s) {
        for (RequestHandler requestHandler : requestHandlers) {
            Object o = requestHandler.handle(s);
            if (o != null) {
                return o;
            }
        }
        return null;
    }
}
